package pc;
import lejos.pc.comm.*;

import java.io.*;

public class BTControl {

	/**
	 * @param args
	 * @throws NXTCommException 
	 */
	public static void main(String[] args) throws NXTCommException {
		
		if (args.length != 2) {
			System.out.println("Usage: BTControl <name> <address>");
			System.exit(1);
		}
		
		NXTComm comm = null;
		try {
			comm = NXTCommFactory.createNXTComm(NXTCommFactory.BLUETOOTH);
		} catch (NXTCommException e) {
			System.out.println("Error during BT load");
			System.out.println(e.getMessage());
			System.exit(2);
		}
		
		NXTInfo info = new NXTInfo(args[0], args[1]);
		
		System.out.println("Connected to " + info.name);
		
		NXTInfo[] infos = comm.search(args[0], NXTCommFactory.BLUETOOTH);
		info = infos[0];
		try {
			comm.open(info);
		} catch (NXTCommException e) {
			System.out.println("Exception during open");
			System.out.println(e.getMessage());
			System.exit(2);
		}
		
		DataOutputStream dos = new DataOutputStream(comm.getOutputStream());
		DataInputStream dis = new DataInputStream(comm.getInputStream());
		
		System.out.println("Sending: 10");
		try {
			dos.writeInt(10);
		} catch (IOException e) {
			System.out.println("Can't send data");
		}
		int i = 0;
		try {
			i = dis.readInt();
		} catch (IOException e) {
			System.out.println("Can't get data");
		}
		System.out.println("Got data: " + Integer.toString(i));
		
		
		try {
			dis.close();
			dos.close();
			comm.close();
		} catch (IOException e) {
			
		}
		
	}

}
